package com.tcy.netlogin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseOpera extends SQLiteOpenHelper
{

	private SQLiteDatabase db;
	private static final String DB_NAME ="database.db";
	private static final String TB_NAME = "info";
	private static final String CREATE_TBL="create table "+TB_NAME+"(username, password, loginURL, logoutURL, autoLogin, autoLogout)";
	
	public DatabaseOpera(Context context) {
		super(context, DB_NAME, null, 2);
	}

	public Info query()
	{
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(TB_NAME, null, null, null, null, null, null);
		if(c.getCount() == 0)
			return null;
		Info data = new Info();
		c.moveToNext();
		data.username = c.getString(0);
		data.password = c.getString(1);
		data.loginURL = c.getString(2);
		data.logoutURL = c.getString(3);
		data.autoLogin = c.getString(4);
		data.autoLogout = c.getString(5);
		db.close();
		return data;
	}
	
	public void insert(Info data)
	{
		del();
		ContentValues values = new ContentValues();
		SQLiteDatabase db = getWritableDatabase();
		
		values.put("username", data.username);
		values.put("password", data.password);
		values.put("loginURL", data.loginURL);
		values.put("logoutURL", data.logoutURL);
		values.put("autoLogin", data.autoLogin);
		values.put("autoLogout", data.autoLogout);
		db.insert(TB_NAME, null, values);
		db.close();
	}
	
	public void update()
	{
		
	}
	
	public void del()
	{
		SQLiteDatabase db = getWritableDatabase();
		db.delete(TB_NAME, null, null);
		db.close();
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		db.execSQL(CREATE_TBL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	public void close()
	{
		if(db != null)
			db.close();
	}
}